#ifndef REMOTEIMAGEVIEW_H
#define REMOTEIMAGEVIEW_H

#include <map>
#include <vector>

#include <QGraphicsView>
#include <QTimer>
#include <QMutex>
#include <QHostInfo>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include "clienttransfer.h"
#include "timestamp.h"
#include "imageview.h"

class RemoteImageView : public ImageView
{
    Q_OBJECT
public:
    explicit RemoteImageView(QWidget *parent = 0);
    
signals:
    void imageTransferProgress(int progess);
    void changeDirection(int x, int y, int z);
    void imgRequest(int timestamp, int seq, int width, int height, int once_count);
    void trackRequest(int timestamp, int x1, int y1, int x2, int y2);
    void ptzRequest(int x, int y, int z);
    void setTransferServer(QHostAddress addr, quint16 port);
    void frameRate(int fps);
    void showSaveFolder(QString);
public slots:
    void changeTargetDirection(int x, int y, int z);
    void setServer(QString addr, int port);
    void transferState(int s);
    void recvResponse(QByteArray response);
    void recvImageData(QByteArray image_data);
    void setImgOnceCount(int c){
        this->m_img_once_count = c;
    }
    void reqestTimerAction();

    void setAddressString(QString addr);
    void setAddress();

    void statisticsTimer();
    void setSaveFolder();
    void setSaveState(int s);

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
private:
    bool m_transfer_state;
    TimeStamp m_last_ans_ts;



    //for img
    void resetImg(unsigned int timestamp, int size);
    void restructImg(unsigned char* data, int size, int seq);

    cv::Mat m_img;
    TimeStamp m_img_ts;
    int m_img_width;
    int m_img_height;
    int m_img_size;
    int m_img_data_offset;
    std::map<int, std::vector<unsigned char> > m_img_temp;
    std::vector<unsigned char> m_img_data;
    int m_img_seq;
    int m_img_total_seq;
    int m_img_once_count;
    QMutex m_req_mutex;

    QTimer m_req_timer;
    TimeStamp m_last_data_ts;

    //for Statistics
    int m_frame_cost;
    TimeStamp m_frame_ts;

    int m_frames;
    int m_sec;
    int m_useful_data;
    int m_useless_data;
    int m_req_times;
#define STATISTICS_INTERVAL 500
    QTimer m_stat_timer;


    //fro track
    int m_ori_width;
    int m_ori_height;
    TimeStamp m_display_ts;
    QMutex m_display_mutex;
    bool m_mouse_pressed;
    bool m_display_new;
    cv::Point2i m_track_pt1;
    cv::Point2i m_track_pt2;
    bool m_show_selected;
    ClientTransfer m_transfer;

    TimeStamp m_track_ts;
    cv::Point2i m_track_ans_pt1;
    cv::Point2i m_track_ans_pt2;
    cv::Point2i m_track_center;

    //for address
    QString m_addr_str;
    QHostAddress m_addr;

    //for save

    void startSaveVideo();
    void stopSaveVideo();
    void saveImg2Video(cv::Mat img);
    QString m_save_folder;
    QString m_save_file;
    cv::VideoWriter m_save_writer;
    bool m_save_state;
    QMutex m_save_mutex;


};

#endif // REMOTEIMAGEVIEW_H
